package com.example.kliven.because.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.example.kliven.because.R;
import com.example.kliven.because.Singleton.VolleySingleton;
import com.example.kliven.because.bean.InterestingBean;

import java.util.List;

/**
 * Created by Kliven on 16/1/15.
 */
public class InterestingAdapter extends BaseAdapter {

    private List<InterestingBean> interestingBeans;
    private Context context;

    public InterestingAdapter(List<InterestingBean> interestingBeans, Context context) {
        this.interestingBeans = interestingBeans;
        this.context = context;
        VolleySingleton.getInstance();
    }

    @Override
    public int getCount() {
        return interestingBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return interestingBeans;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public View getView(int i, View view, ViewGroup viewGroup) {
        InterestViewHodler interestViewHodler;
        String img = interestingBeans.get(i).getMain_img_url();
        if (view == null){
            view = LayoutInflater.from(context).inflate(R.layout.item_lv_interesting,viewGroup,false);
            interestViewHodler = new InterestViewHodler(view);
            view.setTag(interestViewHodler);
        } else {
            interestViewHodler = (InterestViewHodler) view.getTag();
        }

        interestViewHodler.tv_summary.setText(interestingBeans.get(i).getSummary());
        interestViewHodler.iv_img_url.setImageUrl(img,VolleySingleton.getImageLoader());
        return view;
    }

    class InterestViewHodler{
        TextView tv_summary;
        NetworkImageView iv_img_url;

        public InterestViewHodler(View view) {
            tv_summary = (TextView) view.findViewById(R.id.tv_summary);
            iv_img_url = (NetworkImageView) view.findViewById(R.id.net_img_interest);
        }
    }
}
